//
//  Diary.m
//  VideoDiaryClass
//
//  Created by Wang Haitao on 15/10/19.
//  Copyright © 2015年 qianfeng. All rights reserved.
//

#import "Diary.h"

@interface Diary ()

@property (nonatomic, readonly, weak) NSString * basePath;


@end

@implementation Diary

- (Diary *)copyWithZone:(NSZone *)zone
{
    Diary * diary = [Diary new];
    diary.createDate = self.createDate;
    diary.color = self.color;
    diary.duration = self.duration;
    diary.type = self.type;
    diary.content = self.content;
    
    return diary;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    
    self.createDate = [aDecoder decodeObjectForKey:@"CreateDate"];
    self.content = [aDecoder decodeObjectForKey:@"Content"];
    self.color = [aDecoder decodeObjectForKey:@"Color"];
    
    self.duration = [aDecoder decodeFloatForKey:@"Duration"];
    self.type = [aDecoder decodeIntegerForKey:@"Type"];
    
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.createDate forKey:@"CreateDate"];
    [aCoder encodeObject:self.color forKey:@"Color"];
    [aCoder encodeObject:self.content forKey:@"Content"];
    
    [aCoder encodeFloat:self.duration forKey:@"Duration"];
    [aCoder encodeInteger:self.type forKey:@"Type"];
}

- (NSString *)basePath
{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return paths[0];
}

- (NSString *)identifier
{
    NSDateFormatter * df = [NSDateFormatter new];
    [df setDateFormat:@"yyMMddHHmmss"];
    return [df stringFromDate:self.createDate];
}

- (NSString *)displayTime
{
    
    NSDateFormatter * df = [NSDateFormatter new];
    [df setDateFormat:@"yyyy-MM-dd HH:mm"];
    return [df stringFromDate:self.createDate];
}

- (NSString *)videoPath
{
    return [[self.basePath stringByAppendingPathComponent:self.identifier] stringByAppendingPathExtension:@"mp4"];
}

- (NSString *)imagePath
{
    return [[self.basePath stringByAppendingPathComponent:self.identifier] stringByAppendingPathExtension:@"png"];
}

- (void) clear
{
    [[NSFileManager defaultManager] removeItemAtPath:self.imagePath error:nil];
//    [[NSFileManager defaultManager] removeItemAtPath:self.videoPath error:nil];
}

@end
